package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.dto.CmorderDto;
import com.dingreading.cloud.admin.entity.Cmorders;
import com.dingreading.cloud.admin.entity.table.CmordersTableDef;
import com.dingreading.cloud.admin.mapper.CmordersMapper;
import com.dingreading.cloud.admin.service.CmordersService;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.dingreading.cloud.admin.entity.table.MembersTableDef.members;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CmordersServiceImpl extends BaseServiceImpl<CmordersMapper, Cmorders, CmordersTableDef> implements CmordersService {

    @Override
    protected CmordersTableDef getTable() {
        return CmordersTableDef.cmorders;
    }


    @Override
    public PageDto<CmorderDto> page(
            PageUtil pageUtil, List<String> selfAndJoinCorpUidList, String storeUid, String keyword, List<String> orderType,
            Integer status, String beginDate, String endDate, String checkBeginDate, String checkEndDate,
            Double orderAmount
    ) {
        //设置其他参数
        Map<String, Object> otherParams = new HashMap<>();
        otherParams.put("corpUidList", selfAndJoinCorpUidList);
        otherParams.put("storeUid", storeUid);
        otherParams.put("keyword", keyword);
        otherParams.put("orderType", orderType);
        otherParams.put("status", status);
        otherParams.put("orderAmount", orderAmount);
        otherParams.put("beginDate", beginDate);
        otherParams.put("endDate", endDate);
        otherParams.put("checkBeginDate", checkBeginDate);
        otherParams.put("checkEndDate", checkEndDate);

        Page<CmorderDto> page = getMapper().xmlPaginate("cmorderPageXml", Page.of(pageUtil.getPage(), pageUtil.getRows()), otherParams);

        long count = page.getTotalRow();
        List<CmorderDto> list = page.getRecords();

        PageDto<CmorderDto> data = new PageDto<>();
        data.setTotal(count);
        data.setList(list);
        return data;
    }

    @Override
    public Cmorders getByOrderCode(String storeUid, String memberUid, String orderCode) {
        QueryCondition condition = table.orderCode.eq(orderCode);
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(memberUid))
            condition.and(table.memberUid.eq(memberUid));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public CmorderDto getDtoByOrderCode(String storeUid, String memberUid, String orderCode) {
        QueryCondition condition = table.orderCode.eq(orderCode);
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(memberUid))
            condition.and(table.memberUid.eq(memberUid));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition);
        return getMapper().selectOneByQueryAs(wrapper, CmorderDto.class);
    }

    @Override
    public boolean updateStatus(String orderCode, Integer status, String statusName) {
        return UpdateChain.of(Cmorders.class)
                .set(table.status, status)
                .set(table.statusName, statusName)
                .where(table.orderCode.eq(orderCode))
                .update();
    }

    @Override
    public boolean updateStatusByOrderCods(List<String> orderCodes, Integer status, String statusName) {
        return UpdateChain.of(Cmorders.class)
                .set(table.status, status)
                .set(table.statusName, statusName)
                .where(table.orderCode.in(orderCodes))
                .update();
    }

    @Override
    public List<CmorderDto> dotListByOrderCodeIn(List<String> orderCodeList) {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.allColumns, members.memberName.as("newMemberName"))
                .from(table)
                .leftJoin(members).on(table.memberUid.eq(members.uid))
                .where(table.orderCode.in(orderCodeList))
                .orderBy(table.storeUid.asc(), table.memberUid.asc());
        return getMapper().selectListByQueryAs(wrapper, CmorderDto.class);
    }

    @Override
    public List<Cmorders> listByOrderCodeIn(List<String> orderCodeList) {
        return getMapper().selectListByCondition(table.orderCode.in(orderCodeList));
    }

}
